package com.example.demo.design_pattern.singleton;


import java.io.ObjectStreamException;
import java.io.Serializable;

/**
 * @author xkx
 * @description
 * @since 2019/3/20 11:17
 */
public class Singleton implements Serializable, Cloneable {

  private static volatile Singleton singleton = new Singleton();

  private Singleton() {
    getSingleton();
  }

  @Override
  protected Object clone() throws CloneNotSupportedException {
    return getSingleton();
  }

  public static Singleton getSingleton() {
    if (null == singleton) {
      synchronized (Singleton.class) {
        if (null == singleton) {
          singleton = new Singleton();
        }
      }
    }
    return singleton;
  }

  private Object readResolve() throws ObjectStreamException {
    return getSingleton();
  }
}
